package org.gruposp2p.dnie.client.util;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HTML;
import org.gruposp2p.dnie.client.ui.panel.MainPanel;

/**
 *
 * @author jj
 */
public class URLConstants {

    private static String DOCUMENT_TO_SIGN_BASE_PATH = "rest/dnieDocumentToSign/";
    private static String USER_DOCUMENTS_TO_SIGN_BASE_PATH = "rest/private/userUploadedDocuments";
    //private static String USER_DOCUMENTS_TO_SIGN_BASE_PATH = "rest/userUploadedDocuments";
    private static String USER_SIGNED_DOCUMENTS_BASE_PATH = "rest/private/userSignedDocuments";
    //private static String USER_SIGNED_DOCUMENTS_BASE_PATH = "rest/userSignedDocuments";
    private static String LAST_DOCUMENTS_TO_SIGN_BASE_PATH = "rest/lastDocuments";
    private static String MOST_SIGNED_DOCUMENTS_BASE_PATH = "rest/mostSignedDocuments";
    private static String SEARCH_DOCUMENTS_BASE_PATH = "rest/documentToSignSearcher";
    private static String STATISTIC_BASE_PATH = "rest/statistic";
    public static String PATH;
    public static String HOST_NAME;
    public static String PRIVATE_ZONE_URL = getPrivateUrl();
    public static String PUBLIC_ZONE_URL = getPublicUrl();
    public static String SELECTED_SECTION;
        
    public static String getDocumentToSignUrl (String signatureDocumentID) {
        return getApplicationPath() + DOCUMENT_TO_SIGN_BASE_PATH + signatureDocumentID;
    }
    
    public static String getApplicationPath () {
    	String applicationPath = "http://" +
		com.google.gwt.user.client.Window.Location.getHostName();
        //TODO
        if (com.google.gwt.user.client.Window.Location.getPort().equals("8080")) {
            applicationPath += ":8080";
        }
        String totalPath = com.google.gwt.user.client.Window.Location.getPath();
        int indexOfSecondSlash = totalPath.indexOf("/", 1);
        String path = "";
        if (indexOfSecondSlash != -1) {
            path = totalPath.substring(0, indexOfSecondSlash);
        }
        applicationPath = applicationPath + path + "/";
        return applicationPath;
    }
    
    public static String getDocumentToSignJNLPFile (String signatureDocumentID) {
    	String result = getApplicationPath() + "jnlpFile.jsp?document="  
    		+ getDocumentToSignUrl(signatureDocumentID);  
    	return result;
    }
    
    public static String getDocumentToSignBaseUrl () {
        return GWT.getHostPageBaseURL() + DOCUMENT_TO_SIGN_BASE_PATH;
    }    

    public static String getUserDocumentsToSignPostUrl () {
        return GWT.getHostPageBaseURL() + USER_DOCUMENTS_TO_SIGN_BASE_PATH;
    }

    public static String getUserDocumentsToSignGetUrl (int firstResult, int numberResults) {
        return GWT.getHostPageBaseURL() + USER_DOCUMENTS_TO_SIGN_BASE_PATH +
                "?firstResult="+ firstResult + "&numberResults=" + numberResults;
    }

    public static String getUserSignedDocumentsGetUrl (int firstResult, int numberResults) {
        return GWT.getHostPageBaseURL() + USER_SIGNED_DOCUMENTS_BASE_PATH +
                "?firstResult="+ firstResult + "&numberResults=" + numberResults;
    }
    
    public static String getSearchDocumentsGetUrl (
    		String searchText, int firstResult, int numberResults) {
        return GWT.getHostPageBaseURL() + SEARCH_DOCUMENTS_BASE_PATH +
                "?textQuery=" + searchText + "&firstResult="+ firstResult + "&numberResults=" + numberResults;
    }
    
    public static String getLastDocumentsToSignGetUrl (int firstResult, int numberResults) {
        return GWT.getHostPageBaseURL() + LAST_DOCUMENTS_TO_SIGN_BASE_PATH +
                "?firstResult="+ firstResult + "&numberResults=" + numberResults;
    }

    public static String getMostSignedDocumentsToSignGetUrl (int firstResult, int numberResults) {
        return GWT.getHostPageBaseURL() + MOST_SIGNED_DOCUMENTS_BASE_PATH +
                "?firstResult="+ firstResult + "&numberResults=" + numberResults;
    }
    
    public static HTML getDocumentToSingHTMLUrl (String signatureDocumentID) {
        StringBuilder buf = new StringBuilder();
        buf.append("<a href=\"");
        buf.append(getDocumentToSignUrl(signatureDocumentID));
        buf.append("\" TARGET=\"_blank\">");
        buf.append(MainPanel.constants.DocumentUrlCaption() + "</a>");
        return new HTML(buf.toString());    	
    }

    public static String getStatisticGetUrl (String documentToSignId) {
        return GWT.getHostPageBaseURL() + STATISTIC_BASE_PATH +
                "/" + documentToSignId;
    }       

    
    public static String getPrivateUrl () {
    	return "https://" + 
    		com.google.gwt.user.client.Window.Location.getHostName() + ":8443" + 
    		com.google.gwt.user.client.Window.Location.getPath() + 
    		SELECTED_SECTION;
    }

    /*
    public static String getPrivateUrl1 () {
    	return "http://" +
    		com.google.gwt.user.client.Window.Location.getHostName() +
    		com.google.gwt.user.client.Window.Location.getPath() +
    		SELECTED_SECTION;
    }*/

    public static String getPublicUrl () {
    	return "http://" + 
    		com.google.gwt.user.client.Window.Location.getHostName() + 
    		com.google.gwt.user.client.Window.Location.getPath() +
    		SELECTED_SECTION;
    }
    
    //redirect the browser to the given url
    public static native void redirect(String url)/*-{
          $wnd.location = url;
    }-*/;

    public static native void alert(String msg)/*-{
          $wnd.alert(msg);
    }-*/;
    
}
